package mvp.annotation;

import mvp.BasePresenter;
import mvp.IBaseView;

/**
 * @author:ike
 * @function: 注解工厂的实现类
 * @creat time:15:04 2017/12/14
 **/

public class PresenterMvpFactoryImpl<V extends IBaseView, P extends BasePresenter<V>> implements PresenterMvpFactory<V,P>
{
    private Class<P> mPresenterClass;
    private PresenterMvpFactoryImpl(Class<P> mPresenterClass) {
        this.mPresenterClass = mPresenterClass;
    }

    /**
     * 创建代理类工厂的实现类
     * @param clazz
     * @param <V>
     * @param <P>
     * @return
     */
    public static <V extends IBaseView,P extends BasePresenter<V>> PresenterMvpFactoryImpl<V,P> createPresenterFactory(Class<?> clazz){
        PresenterName annotation = clazz.getAnnotation(PresenterName.class);
        Class<P> presenter=null;
        if (annotation!=null){
            presenter= (Class<P>) annotation.value();
            
        }
        if (presenter==null){
            return null;
        }else {
           return new PresenterMvpFactoryImpl<>(presenter); 
        }
    }
    @Override
    public P createPresenter() {
        try {
            return mPresenterClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
           new RuntimeException("未发现代理类的注解信息,检查是否使用了@PresenterName(xxx.class)注解");
        }
        return null;
    }
}
